﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Transactions;

namespace BusinessLogic
{
    public static class HDBanLe
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.HDBanLe[] Select()
        {
            Entities.HDBanLe[] list = null;
            try
            {
                string sql = Common.Constants.HDBanLe.SelectHDBanLe;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HDBanLe hdbanle = new Entities.HDBanLe();
                    hdbanle.HDBanLeID = int.Parse(dr[Common.Constants.HDBanLe.HDBanLeID].ToString());
                    hdbanle.MaHDBanLe = dr[Common.Constants.HDBanLe.MaHDBanLe].ToString();
                    hdbanle.NgayLap = DateTime.Parse(dr[Common.Constants.HDBanLe.NgayLap].ToString());
                    hdbanle.MaKhachHang = dr[Common.Constants.HDBanLe.MaKhachHang].ToString();
                    hdbanle.TenKhachHang = dr[Common.Constants.HDBanLe.TenKhachHang].ToString();
                    hdbanle.MaKho = dr[Common.Constants.HDBanLe.MaKho].ToString();
                    hdbanle.TenKho = dr[Common.Constants.HDBanLe.TenKho].ToString();
                    hdbanle.MaNhanVien = dr[Common.Constants.HDBanLe.MaNhanVien].ToString();
                    hdbanle.TenNhanVien = dr[Common.Constants.HDBanLe.TenNhanVien].ToString();
                    hdbanle.ChietKhau = dr[Common.Constants.HDBanLe.ChietKhau].ToString();
                    hdbanle.ThueGTGT = dr[Common.Constants.HDBanLe.ThueGTGT].ToString();
                    hdbanle.TongTienThanhToan = dr[Common.Constants.HDBanLe.TongTienThanhToan].ToString();
                    hdbanle.MaTheVip = dr[Common.Constants.HDBanLe.MaTheVip].ToString();
                    hdbanle.GiaTriThe = dr[Common.Constants.HDBanLe.GiaTriThe].ToString();
                    hdbanle.Deleted = bool.Parse(dr[Common.Constants.HDBanLe.Deleted].ToString());
                    arr.Add(hdbanle);
                }
                list = (Entities.HDBanLe[])arr.ToArray(typeof(Entities.HDBanLe));
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.HDBanLe[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert
        public static int InsertUpdate(Entities.HDBanLe hdbanle)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.HDBanLe.InsertUpdateHDBanLe;
                    cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.HanhDong, SqlDbType.NVarChar, 50).Value = hdbanle.HanhDong;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.HDBanLeID, SqlDbType.Int).Value = hdbanle.HDBanLeID;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaHDBanLe, SqlDbType.VarChar, 50).Value = hdbanle.MaHDBanLe;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.NgayLap, SqlDbType.DateTime).Value = hdbanle.NgayLap;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaKhachHang, SqlDbType.VarChar, 50).Value = hdbanle.MaKhachHang;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.TenKhachHang, SqlDbType.NVarChar, 200).Value = hdbanle.TenKhachHang;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaKho, SqlDbType.VarChar, 50).Value = hdbanle.MaKho;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.TenKho, SqlDbType.NVarChar, 200).Value = hdbanle.TenKho;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaNhanVien, SqlDbType.VarChar, 50).Value = hdbanle.MaNhanVien;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.TenNhanVien, SqlDbType.NVarChar, 200).Value = hdbanle.TenNhanVien;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.ChietKhau, SqlDbType.Float).Value = hdbanle.ChietKhau;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.ThueGTGT, SqlDbType.VarChar, 50).Value = hdbanle.ThueGTGT;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.TongTienThanhToan, SqlDbType.Float).Value = hdbanle.TongTienThanhToan;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaTheVip, SqlDbType.VarChar, 50).Value = hdbanle.MaTheVip;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.GiaTriThe, SqlDbType.Float).Value = hdbanle.GiaTriThe;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.Deleted, SqlDbType.Bit).Value = hdbanle.Deleted;
                    rt = cmd.ExecuteNonQuery();
                    cn.Close();
                    if (BusinessLogic.ChiTietHDBanLe.InsertUpdate(hdbanle.DanhSach) == 0)
                    {
                        ts.Dispose();
                        return 0;
                    }
                    if (TruSoLuong(hdbanle) == 0)
                    {
                        ts.Dispose();
                        return 0;
                    }
                    if (InsertUpdate(hdbanle.PhieuTTCuaKH) == 0)
                    {
                        ts.Dispose();
                        return 0;
                    }
                    if (InsertUpdate(hdbanle.ChiTietPhieuTTCuaKH) == 0)
                    {
                        ts.Dispose();
                        return 0;
                    }
                    if (hdbanle.TheVip != null)
                    {
                        if (BusinessLogic.TheVip.InsertUpdate(hdbanle.TheVip) == 0)
                        {
                            ts.Dispose();
                            return 0;
                        }
                    }
                    if (hdbanle.DiemThuongKhachHang != null)
                    {
                        if (BusinessLogic.DiemThuongKhachHang.InsertUpdate(hdbanle.DiemThuongKhachHang) == 0)
                        {
                            ts.Dispose();
                            return 0;
                        }
                    }
                    LuuVietHeThong.LuuViet(
                        "Thêm hóa đơn",
                        hdbanle.TaiKhoan.MaNhanVien,
                        "Thêm hóa đơn mới " + hdbanle.MaHDBanLe,
                        hdbanle.TaiKhoan.TenDangNhap,
                        DateTime.Now.ToString("dd/MM/YYYY"));
                    ts.Complete();
                }
                return rt;
            }
            catch
            {
                ts.Dispose();
                return 0;
            }

        }

        #region thêm phiếu thanh toán của khách hàng
        public static int InsertUpdate(Entities.PhieuTTCuaKH phieuthanhtoankhachhang)
        {
            try
            {
                string sql = Common.Constants.PhieuTTCuaKH.InsertUpdatePhieuTTKH;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);

                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.HanhDong, SqlDbType.NVarChar, 20).Value = phieuthanhtoankhachhang.HanhDong;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.PhieuTTCuaKHID, SqlDbType.Int).Value = phieuthanhtoankhachhang.PhieuTTCuaKHID;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaPhieuTTCuaKH, SqlDbType.VarChar, 50).Value = phieuthanhtoankhachhang.MaPhieuTTCuaKH;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NgayLap, SqlDbType.DateTime).Value = phieuthanhtoankhachhang.NgayLap;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaKhachHang, SqlDbType.VarChar, 50).Value = phieuthanhtoankhachhang.MaKhachHang;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.TenKhachHang, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.TenKhachHang;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaNhanVien, SqlDbType.NVarChar, 50).Value = phieuthanhtoankhachhang.MaNhanVien;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaKho, SqlDbType.NVarChar, 50).Value = phieuthanhtoankhachhang.MaKho;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.TongThanhToan, SqlDbType.Float).Value = phieuthanhtoankhachhang.TongThanhToan;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NoHienThoi, SqlDbType.Float).Value = phieuthanhtoankhachhang.NoHienThoi;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NguoiNop, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.NguoiNop;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.LoaiPhieu, SqlDbType.Bit).Value = phieuthanhtoankhachhang.LoaiPhieu;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.GhiChu, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.GhiChu;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.Deleted, SqlDbType.Bit).Value = phieuthanhtoankhachhang.Deleted;
                int i = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                return i;
            }
            catch
            {
                return 0;
            }
        }
        #endregion

        #region thêm chi tiết phiếu thanh toán của khách hàng
        public static int InsertUpdate(Entities.ChiTietPhieuTTCuaKH chitietphieuttkh)
        {
            try
            {
                string sql = Common.Constants.ChiTietPhieuTTKH.InsertUpdate;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);

                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.HanhDong, SqlDbType.NVarChar, 50).Value = chitietphieuttkh.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.MaHoaDon, SqlDbType.VarChar, 50).Value = chitietphieuttkh.MaHDBanHang;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.MaPhieuTTCuaKH, SqlDbType.VarChar, 50).Value = chitietphieuttkh.MaPhieuTTCuaKH;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.TongTien, SqlDbType.Float).Value = chitietphieuttkh.TongTienThanhToan;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.TienNo, SqlDbType.Float).Value = chitietphieuttkh.SoNo;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.ThanhToan, SqlDbType.Float).Value = chitietphieuttkh.ThanhToan;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.GhiChu, SqlDbType.NVarChar, 200).Value = "";
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.Deleted, SqlDbType.Bit).Value = false;
                int i = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                return i;
            }
            catch
            {
                return 0;
            }
        }

        public static int InsertUpdate(Entities.ChiTietPhieuTTCuaKH[] chitietphieuttkh)
        {
            try
            {
                for (int i = 0; i < chitietphieuttkh.Length; i++)
                {
                    if (InsertUpdate(chitietphieuttkh[i]) == 0)
                        return 0;
                }
                return 1;
            }
            catch
            {
                return 0;
            }

        }
        #endregion

        #region trừ số lương
        public static int TruSoLuong(Entities.HDBanLe hdbanle)
        {
            try
            {
                for (int k = 0; k < hdbanle.DanhSachTruKho.Length; k++)
                {
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(Common.Constants.HDBanLe.sp_TruKho, cn);
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaKho, SqlDbType.VarChar).Value = hdbanle.DanhSachTruKho[k].MaKho;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaHangHoa, SqlDbType.VarChar).Value = hdbanle.DanhSachTruKho[k].MaHangHoa;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.SoLuong, SqlDbType.Float).Value = hdbanle.DanhSachTruKho[k].SoLuong;
                    int z = cmd.ExecuteNonQuery();
                    cn.Close();
                    if (z != 0)
                    {
                        continue;
                    }
                    else
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        #endregion

        #endregion
    }
}
#endregion